package com.manage.web.controller;

import com.manage.model.dto.OrderDTO;
import com.manage.service.MoreDataSourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 多数据源操作
 * @author duaiyu
 * @version V1.0
 * @date 2022/5/29
 * 来源：自研
 */
@Slf4j
@Controller
@RequestMapping(value = "/moreDataSourceController")
public class MoreDataSourceController {
    @Resource
    MoreDataSourceService moreDataSourceService;
    /**
     * 查询primary数据库的数据
     */
    @ResponseBody
    @RequestMapping(value = "/queryPrimaryOrder", method = RequestMethod.GET)
    public List<OrderDTO> queryPrimaryOrder(){
        return moreDataSourceService.queryPrimaryOrder();
    }

    /**
     * 查询secondary数据库的数据
     */
    @ResponseBody
    @RequestMapping(value = "/querySecondaryOrder", method = RequestMethod.GET)
    public List<OrderDTO> querySecondaryOrder(){
        return moreDataSourceService.querySecondaryOrder();
    }

    /**
     * 新增primary数据库的数据
     */
    @ResponseBody
    @RequestMapping(value = "/insertPrimary", method = RequestMethod.POST)
    public String insertPrimary(@RequestBody OrderDTO order){
        // 0失败   1成功
        Integer result = moreDataSourceService.insertPrimary(order);
        if(result == 0){
            return "添加数据失败";
        }else{
            return "添加数据成功";
        }
    }

    /**
     * 新增secondary数据库的数据
     */
    @ResponseBody
    @RequestMapping(value = "/insertSecondary", method = RequestMethod.POST)
    public String insertSecondary(@RequestBody OrderDTO order){
        // 0失败   1成功
        Integer result = moreDataSourceService.insertSecondary(order);
        if(result == 0){
            return "添加数据失败";
        }else{
            return "添加数据成功";
        }
    }

    /**
     * 把主数据库的数据落到第二个数据库
     */
    @ResponseBody
    @RequestMapping(value = "/dataCopy", method = RequestMethod.GET)
    public String dataCopy(){
        // 0失败   1成功
        Integer result = moreDataSourceService.dataCopy();
        if(result == 0){
            return "添加数据失败：" + result;
        }else{
            return "添加数据成功";
        }
    }
}
